package 学生管理系统升级版;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class 登录 {
    public static void main(String[] args) {
        //创建用户数组
        ArrayList<User> list = new ArrayList<>();

        loop:
        while (true) {
            //主界面
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作1登录 2注册 3忘记密码");
            //用户选择
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入你的选择：");
            int select = sc.nextInt();
            switch (select) {
                case 1:
                    login(list);
                    break;
                case 2:
                    regiser(list);
                    break;
                case 3:
                    forgetPassword(list);
                    break;
                case 4:
                    System.out.println("退出");
                    break loop;
                default:
                    System.out.println("没有这个选项");
            }
        }


    }

    private static void forgetPassword(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = sc.next();
        boolean flag = contain2(list, username);
        if (!flag) {
            System.out.println("当前用户未注册，请先注册");
            return;
        }
        System.out.println("请输入身份证号码");
        String userid = sc.next();
        System.out.println("请输入手机号码");
        String phonenumber = sc.next();
        int index = findIndex(list, username);
        User user = list.get(index);
        if (!(user.getUserId().equals(userid) && user.getPhoneNumber().equals(phonenumber))) {
            System.out.println("身份证号码或者手机号码输入有误，请重新输入");
            return;
        }
        String password;
        while (true) {
            System.out.println("请输入新密码");
            password = sc.next();
            System.out.println("请再次输入新密码");
            String againpassword = sc.next();
            if (password.equals(againpassword)) {
                System.out.println("两次密码输入一致");
                break;
            } else {
                System.out.println("两次密码输入不一致");
                continue;
            }
        }
        user.setPassWord(password);
    }


    private static int findIndex(ArrayList<User> list, String username) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if (user.getUserName().equals(username)) {
                return i;
            }
        }
        return -1;
    }

    //用户注册
    private static void regiser(ArrayList<User> list) {
        Scanner s = new Scanner(System.in);
        String UserName;
        String Password;
        String userId;
        String phoneNumber;
        //键盘录入用户名
        while (true) {
            System.out.println("请输入你要注册的用户名");
            UserName = s.next();
            boolean flag1 = contain1(UserName);
            if (!flag1) {
                System.out.println("条件格式不满足要求，请重新输入");
                continue;
            }
            boolean flag2 = contain2(list, UserName);
            if (flag2) {
                System.out.println("用户名" + UserName + "已经存在请重新输入");
            } else {
                System.out.println("用户名" + UserName + "可用");
                break;
            }

        }
        //键盘录入密码
        while (true) {
            System.out.println("请输入要注册的密码");
            Password = s.next();
            System.out.println("请再次输入密码");
            String againpassword = s.next();
            if (!Password.equals(againpassword)) {
                System.out.println("两次输入的密码不一致，请重新输入。");
                continue;
            } else {
                System.out.println("该密码可用,继续录入其他数据");
                break;
            }
        }
        //键盘录入身份证号码
        while (true) {
            System.out.println("请输入身份证号码：");
            userId = s.next();
            boolean flag3 = checkId(userId);
            if (flag3) {
                System.out.println("身份证号码输入正确，请继续录入");
                break;
            } else {
                System.out.println("身份证号码输入错误，请重新输入。");
                continue;
            }
        }
        //键盘录入手机号码
        while (true) {
            System.out.println("请输入手机号码：");
            phoneNumber = s.next();
            boolean flag4 = checkphoneNumber(phoneNumber);
            if (flag4) {
                System.out.println("手机号输入正确");
                break;

            } else {
                System.out.println("手机号输入错误，请重新输入");
                continue;
            }
        }

        User u = new User(UserName, Password, userId, phoneNumber);
        list.add(u);
        System.out.println("用户注册完成，请返回登录。");


    }

    //验证电话号码是否正确
    private static boolean checkphoneNumber(String phoneNumber) {
        int len = phoneNumber.length();
        //长度为11位
        if (len != 11) {
            return false;
        }
        //不能以零开头
        if (phoneNumber.startsWith("0")) {
            return false;
        }
        //都是数字
        for (int i = 0; i < phoneNumber.length(); i++) {
            char c = phoneNumber.charAt(i);
            if (!(c > '0' && c < '9')) {
                return false;
            }
        }
        return true;
    }

    //验证userid
    private static boolean checkId(String userId) {
        int len = userId.length();
        if (!(len == 18)) {
            return false;
        }
        if (userId.startsWith("0")) {
            return false;
        }
        for (int i = 0; i < userId.length() - 1; i++) {
            char c = userId.charAt(i);
            if (!(c <= '9' && c >= '0')) {
                return false;
            }
        }
        char a = userId.charAt(userId.length() - 1);
        if ((a == 'x') || (a == 'X') || (a >= '0' && a <= '9')) {
            return true;

        } else {
            return false;
        }
    }


    //验证用户名是否唯一
    private static boolean contain2(ArrayList<User> list, String userName) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            String rightusername = user.getUserName();
            if (rightusername.equals(userName)) {
                return true;
            }
        }
        return false;
    }


    //检验username是否符合要求
    private static boolean contain1(String UserName) {
        int len = UserName.length();
        if (len < 3 || len > 15) {
            return false;
        }
        for (int i = 0; i < UserName.length(); i++) {
            char c = UserName.charAt(i);
            if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9'))) {
                return false;
            }
        }
        int count = 0;
        for (int i = 0; i < UserName.length(); i++) {
            char c = UserName.charAt(i);
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                count++;
                break;
            }
        }
        return count > 0;
    }

    //登录用户
    private static void login(ArrayList<User> list) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            System.out.println("请输入用户名：");
            String username = sc.next();
            boolean flag = contain2(list, username);
            if (!flag) {
                System.out.println("用户名" + username + "未注册，，请先注册后再登录。");
                return;
            }
            System.out.println("请输入密码：");
            String Password = sc.next();

            while (true) {
                String rightcode = getCode();
                System.out.println("当前正确验证码为:" + rightcode);
                System.out.println("请输入验证码：");
                String code = sc.next();
                if (code.equalsIgnoreCase(rightcode)) {
                    System.out.println("验证码正确");
                    break;
                } else {
                    System.out.println("验证码错误");
                    continue;
                }
            }
            User useInfo = new User(username, Password, null, null);
            boolean result = checkUserInfo(list, useInfo);
            if (result) {
                System.out.println("登录成功");
                guanlixit ss=new guanlixit();
                ss.startStudentSystem();
            } else {
                System.out.println("登录失败");
                if (i == 2) {
                    System.out.println("当前用户已经被锁定，请联系管理员");
                } else {
                    System.out.println("用户名或密码错误，还剩" + (2 - i) + "次机会。");
                }
            }

        }
    }

    //检验用户名和密码是否正确
    private static boolean checkUserInfo(ArrayList<User> list, User useInfo) {
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            if (user.getUserName().equals(useInfo.getUserName()) && user.getPassWord().equals(useInfo.getPassWord())) {
                return true;
            }
            return false;
        }
        return false;
    }

    //生成验证码
    private static String getCode() {
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            list.add((char) ('a' + i));
            list.add((char) ('A' + i));
        }
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(list.size());
            char c = list.get(index);
            sb.append(c);
        }
        int number = r.nextInt(10);
        sb.append(number);

        char[] arr = sb.toString().toCharArray();
        int randomIndex = r.nextInt(arr.length);
        char temp = arr[randomIndex];
        arr[randomIndex] = arr[arr.length - 1];
        arr[arr.length - 1] = temp;
        return new String(arr);
    }
}
